package com.example.demo.services.impl;

import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.dao.UserMapper;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.User;
import com.example.demo.services.IUserService;
import com.example.demo.utils.MD5Util;
import com.example.demo.utils.StringUtil;
import com.example.demo.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void createNormalUser(User user) {
        //1.非空效验
        if(user==null || StringUtil.isEmpty(user.getUsername()) ||
                StringUtil.isEmpty(user.getPassword()) || StringUtil.isEmpty(user.getNickname())
        || StringUtil.isEmpty(user.getSalt()))
        {

            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.按照用户名查询信息
        User existsUser=userMapper.selectByUserName(user.getUsername());
        //判断用户是否存在
        if(existsUser!=null)
        {
            //打印日志
            log.info(ResultCode.FAILED_USER_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_EXISTS));

        }

        //3.新增用户流程，设置默认值
        user.setGender((byte) 2);
        user.setArticleCount(0);
        user.setIsAdmin((byte) 0);
        user.setState((byte) 0);
        user.setDeleteState((byte) 0);

        Date date=new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);

        //写入数据库
        int row=userMapper.insertSelective(user);
        if(row!=1)
        {
            //打印日志
            log.info(ResultCode.FAILED_CREATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
            //打印日志
        log.info("新用户成功,username="+user.getUsername()+" . ");
    }

    @Override
    public User selectByUserName(String username) {

        if(StringUtil.isEmpty(username))
        {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //返回查询结果
        return userMapper.selectByUserName(username);
    }

    @Override
    public User login(String username, String password) {
        //1.非空效验
        if(StringUtil.isEmpty(username) || StringUtil.isEmpty(password))
        {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }
        //2.根据用户名查询用户
        User user=selectByUserName(username);
        //判断用户非空效验
        if(user==null)
        {
            //打印日志
            log.warn(ResultCode.FAILED_LOGIN.toString()+",username="+username);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }

        //获取加密密码
        String  encryptPassword= MD5Util.md5salt(password,user.getSalt());

        //判断登录密码和注册密码是否一样
        if(!encryptPassword.equalsIgnoreCase(user.getPassword()))
        {
            //打印日志
            log.warn(ResultCode.FAILED_LOGIN.toString()+"密码错误,username="+username);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }
        //登录成功
        log.info("登录成功，username="+username);
        return user;
    }

    @Override
    public User selectById(Long id) {
        //1.非空效验
        if(id==null)
        {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }
        //2.根据用户名查询用户
        User user=userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if(id==null || id<=0)
        {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        User user=userMapper.selectByPrimaryKey(id);
        if(user==null)
        {
            log.warn(ResultCode.ERROR_IS_NULL.toString()+",User id="+id);
            throw  new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }

        User updateUser=new User();
        updateUser.setId(user.getId());
        updateUser.setArticleCount(user.getArticleCount()+1);
        int row=userMapper.updateByPrimaryKeySelective(updateUser);
        if(row!=1)
        {
            log.warn(ResultCode.FAILED.toString()+",受影响的行数大于一，操作失败！！！");
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public void subOneArticleCountById(Long id) {
        if(id==null || id<=0)
        {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        User user=userMapper.selectByPrimaryKey(id);
        if(user==null)
        {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString()+",User id="+id);
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        User updateUser=new User();
        updateUser.setId(user.getId());
        updateUser.setArticleCount(user.getArticleCount()-1);
        if(updateUser.getArticleCount()<0)
        {
            updateUser.setArticleCount(0);
        }
        int row=userMapper.updateByPrimaryKeySelective(updateUser);
        if(row!=1)
        {
            log.warn(ResultCode.FAILED.toString()+",受影响的行数大于一，操作失败！！！");
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void modifyInfo(User user) {
        // 1. 非空校验
        if (user == null || user.getId() == null || user.getId() <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 2. 校验用户是否存在
        User existsUser = userMapper.selectByPrimaryKey(user.getId());
        if (existsUser == null) {
            // 打印日志
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        // 3. 定义一个标志位
        boolean checkAttr = false; // false表示没有校验通过

        // 4. 定义一个专门用来更新的对象，防止用户传入的User对象设置了其他的属性
        // 当使用动态SQL进行更新的时候，覆盖了没有经过校验的字段
        User updateUser = new User();
        // 5. 设置用户Id
        updateUser.setId(user.getId());

        // 6. 对每个一个参数进行校验并赋值
        if (!StringUtil.isEmpty(user.getUsername())
                && !user.getUsername().equals(existsUser.getUsername())) {
            // 需要更新用户名(登录名)时，进行唯一性的校验
            User checkUser = userMapper.selectByUserName(user.getUsername());
            if (checkUser != null) {
                // 用户已存在
                log.warn(ResultCode.FAILED_USER_EXISTS.toString());
                // 抛出异常
                throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_EXISTS));
            }
            // 数据库中没有找到相应的用户，表示可以修改用户名
            updateUser.setUsername(user.getUsername());
            // 更新标志位
            checkAttr = true;
        }

        // 7. 校验昵称
        if (!StringUtil.isEmpty(user.getNickname())
                && !user.getNickname().equals(existsUser.getNickname())) {
            // 设置昵称
            updateUser.setNickname(user.getNickname());
            // 更新标志位
            checkAttr = true;
        }
        // 8. 校验性别
        if (user.getGender() != null && user.getGender() != existsUser.getGender()) {
            // 设置性别
            updateUser.setGender(user.getGender());
            // 合法性校验
            if (updateUser.getGender() > 2 || updateUser.getGender() < 0) {
                updateUser.setGender((byte) 2);
            }
            // 更新标志位
            checkAttr = true;
        }
        // 9. 校验邮箱
        if (!StringUtil.isEmpty(user.getEmail())
                && !user.getEmail().equals(existsUser.getEmail())) {
            // 设置邮箱
            updateUser.setEmail(user.getEmail());
            // 更新标志位
            checkAttr = true;
        }
        // 9. 校验电话号码
        if (!StringUtil.isEmpty(user.getPhoneNum())
                && !user.getPhoneNum().equals(existsUser.getPhoneNum())) {
            // 设置电话号码
            updateUser.setPhoneNum(user.getPhoneNum());
            // 更新标志位
            checkAttr = true;
        }
        // 10. 校验个人简介
        if (!StringUtil.isEmpty(user.getRemark())
                && !user.getRemark().equals(existsUser.getRemark())) {
            // 设置电话号码
            updateUser.setRemark(user.getRemark());
            // 更新标志位
            checkAttr = true;
        }
        // 11. 根据标志位来决定是否可以执行更新
        if (checkAttr == false) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 12. 调用DAO
        int row = userMapper.updateByPrimaryKeySelective(updateUser);
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + ", 受影响的行数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void modifyPassword(Long id, String newPassword, String oldPassword) {
        if(id==null || id<=0)
        {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        User user=userMapper.selectByPrimaryKey(id);
        if(user==null || user.getDeleteState()==1){

            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //对老密码进行加密，获取密文
        String oldEncryptPassword=MD5Util.md5salt(oldPassword,user.getSalt());
        //与当前密码进行比较
        if(!oldEncryptPassword.equalsIgnoreCase(user.getPassword()))
        {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //新的盐值
        String salt= UUIDUtil.uuid_32();
        //生成新密码的密文
        String encryptPassword=MD5Util.md5salt(newPassword,salt);

        //构造更新的对象
        User updateUser=new User();
        Date date=new Date();
        updateUser.setUpdateTime(date);
        updateUser.setSalt(salt);
        updateUser.setPassword(encryptPassword);
        updateUser.setId(user.getId());

        //调用dao
        int row=userMapper.updateByPrimaryKeySelective(updateUser);
        if(row!=1)
        {
            log.warn(ResultCode.FAILED.toString()+",受影响的行数大于一，操作失败！！！");
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }



    }
}
